<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 11:44
 */

namespace app\admin\controller;
use       think\Controller;
use       think\Db;
use       think\Request;
vendor('api.weixinapi');
class Wxmanage extends Controller
{
    //图文消息列表
    public function textpic_list()
    {
        $count = Db::name('x_textpic')->count();
        $list = Db::name("x_textpic")->select();
        $this->assign('count', $count);
        $this->assign('list', $list);
        return $this->fetch();
    }

    //添加关注时回复的图文消息
    public function add_textpic_show()
    {
        return $this->fetch();
    }

    //编辑图文消息
    public function edit_textpic()
    {
        $id = Request::instance()->param('id');
        $message = Db::name('x_textpic')->where('id', $id)->find();
        $this->assign('list', $message);
        $this->assign('id', $id);
        return $this->fetch();
    }

    //添加回复的图文消息
    public function add_subcribe()
    {
        $data = Request::instance()->post();
        $data['create_time'] = date('Y-m-d');
        if (isset($_GET['id'])) //如果是编辑模式
        {
            $offest = Db::name('x_textpic')->where('id', $_GET['id'])->update($data);
            if ($offest) {
                return json(['code' => 1, 'msg' => '图文消息编辑成功']);
            }
            return json(['code' => 0, 'msg' => '图文消息编辑失败']);
        } //如果是添加模式
        else {
            if ($data['title'] == null || $data['url'] == null || $data['picurl'] == null) {
                return json(['code' => 0, 'msg' => '添加失败!请填写完整的信息']);
            }
            $offest = Db::name('x_textpic')->insert($data);
            if ($offest) {
                return json(['code' => 1, 'msg' => '图文消息添加成功']);
            }
            return json(['code' => 0, 'msg' => '图文消息添加失败']);
        }
    }

    //删除图文消息
    public function textpic_delete()
    {
        $id = Request::instance()->get('id');
        $offest = Db::name('x_textpic')->where('id', $id)->delete();
        if ($offest) {
            return json(['code' => 1, 'msg' => '图文消息删除成功']);
        }
        return json(['code' => 0, 'msg' => '图文消息删除失败']);
    }

    //一级自定义菜单列表
    public function menu_free_list()
    {
        //获取一级菜单的list
        $list = Db::table('x_weixin_menu')->where('lev', 1)->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //二级自定义菜单列表
    public function menu_free2_list()
    {
        //获取父级的ID
        $pid = Request::instance()->get('pid');
        $list = Db::table('x_weixin_menu')->where('pid', $pid)->select();
        $this->assign('pid', $pid);
        $this->assign('list', $list);
        return $this->fetch();

    }

    //一 二 级菜单添加
    public function add_free1_show()
    {
        return $this->fetch();
    }

    public function add_free2_show()
    {
        $pid = Request::instance()->get('pid');
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    //添加自定义菜单
    public function add_free_menu()
    {
        //获取传过来的数据
        $data = Request::instance()->post();
        $offest = Db::table('x_weixin_menu')->insert($data);
        if ($offest)
            return json(['code' => 1, 'msg' => '添加成功！']);
        return json(['code' => 0,     'msg' => '添加失败请重新添加！']);
    }

    //自定义菜单编辑
    public function makefree()
    {
        return $this->fetch();
    }

    //调用接口生成自定义菜单
    public function wxmenu_make()
    {
        $data=Request::instance()->post();
        $this->create_freemenu($data['freemenu']);
        return  json(['code'=>0,'msg'=>'自定义菜单编辑成功']);

    }
    //启用curl 模仿游览器使用https协议访问链接
    private function http_curl($url,$data=null)
    {
        //1.初始化，创建一个新cURL资源
        $ch = curl_init();
        //2.设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);
        //curl_setopt($ch, CURLOPT_HEADER, 0);
        if(!empty($data)){
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不检测服务器的证书是否由正规浏览器认证过的授权CA颁发
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不检测服务器的域名与证书上的是否一致
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//禁止curl资源直接输出
        //3.抓取URL并把它传递给浏览器
        $opt=curl_exec($ch);
        //4.关闭cURL资源，并且释放系统资源
        curl_close($ch);
        return $opt;
    }
    //获取access_token
    public function getAccess_token()
    {
        $appid="wx624432bdd5d846f3";   //微信公众号唯一标识
        $secret="2734412ee88e2f49b0d312cd32d6b31e";  //秘钥
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $json=$this->http_curl($url);
        $result=json_decode($json);
        return $result->access_token;
    }

    //创建自定义菜单
    public function create_freemenu($data=null)
    {
        //调用数据封装函数
        $token=$this->getAccess_token();
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token}";
        //自定义菜单生成
        $result=$this->http_curl($url,$data);
        var_dump($result);
    }
}